using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using NLog.Web;
using System.Reflection;
using WMS.Infrasturcture;
using WMS.Infrasturcture.Implement;
using WMS.Infrasturcture.Interfaces;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo
    {
        Title = "WMS APIWRITE",
        Version = "v1",
        Description = "仓库管理系统接口文档"
    });

    // 添加 XML 注释（如果你有生成 XML 文档）
    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
    if (File.Exists(xmlPath))
    {
        options.IncludeXmlComments(xmlPath, true);
    }
});

//上下文
//builder.Services.AddDbContext<DbContext>(o => {
//    o.UseMySql(builder.Configuration.GetConnectionString("conn"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("conn")));
//});

builder.Services.AddDbContext<DBcontext>(p =>
{
    p.UseMySql(builder.Configuration.GetConnectionString("conn"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("conn")), action =>
    {
        action.CommandTimeout(60);//命令超时时间 60秒
        //sql链接 错误重试策略 maxRetryCount：最大重试尝试次数。 maxRetryDelay：重试之间的最大延迟。
        action.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(5), errorNumbersToAdd: null);
    }).EnableSensitiveDataLogging();
    //把sql语句 打印到控制台  可以过滤指定的日志级别
    p.UseLoggerFactory(LoggerFactory.Create(configure =>
    {
        configure
        .AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information)
        .AddConsole();
    }));
});


//日志

builder.Host.UseNLog();


builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));

// 添加 AutoMapper（自动扫描当前程序集中的 Profile）
//builder.Services.AddAutoMapper(typeof(UserProfile));
//中介者
builder.Services.AddMediatR(o => o.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));






var app = builder.Build();


//跨域
app.UseCors(o => o.AllowAnyMethod().AllowAnyOrigin().AllowAnyHeader());


// Configure the HTTP request pipeline.

app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WMS APIWRITE v1"));


app.UseAuthorization();

app.MapControllers();

app.Run();
